package com.hhit.bigscreenbackmanage.service.impl.jsgl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.PageUtil;
import com.hhit.bigscreenbackmanage.dao.jsgl.TJsglTysjBdzMapper;
import com.hhit.bigscreenbackmanage.entity.jsgl.TJsglCspsxm;
import com.hhit.bigscreenbackmanage.entity.jsgl.TJsglTysjBdz;
import com.hhit.bigscreenbackmanage.service.jsgl.TJsglTysjBdzService;
import com.hhit.bigscreenbackmanage.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
public class TJsqlTysjBdzServiceImpl implements TJsglTysjBdzService {

    private Logger log = LoggerFactory.getLogger(TJsqlTysjBdzServiceImpl.class);

    @Autowired
    TJsglTysjBdzMapper tJsglTysjBdzMapper;

    @Override
    public Map<String,Object> insertTJsglTysjBdz(MultipartFile[] file, String dataTime,String propName) throws Exception {
        //删除同一时间数据
        JSONObject json = new JSONObject();
        json.put("dataTime",dataTime);
        if("bdzTysjColumnName".equals(propName)) {
            tJsglTysjBdzMapper.deleteTysjBdzRecord(json);
        }else if("bdzTcqkColumnName".equals(propName)){
            tJsglTysjBdzMapper.deleteTcqkBdzRecord(json);
        }

        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TJsglTysjBdz> list = (List<TJsglTysjBdz>) Utils.mapToEntiry(result, TJsglTysjBdz.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TJsglTysjBdz tJsglTysjBdz : list) {
                        tJsglTysjBdz.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tJsglTysjBdz.setDataTime(dataTime);
                    }
                    tJsglTysjBdzMapper.insertTJsglTysjBdz(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String,String> getbdztysjNum(JSONObject jsonObject) throws Exception {
        if("建设分公司".equals(jsonObject.get("sjdw"))){
            jsonObject.put("sjdw", "");
            jsonObject.put("sjdw2", "建设分公司");
        }
        List<Map<String,String>> result = new ArrayList<>();
        result = tJsglTysjBdzMapper.getbdztysjNum(jsonObject);
        if(result.size()>0){
            if(result.get(0) == null){
                return (Map<String, String>) new ArrayList();
            }else {
                return result.get(0);
            }
        }else {
            return (Map<String, String>) new ArrayList();
        }
    }

    @Override
    public Map<String, Object> queryTysjBdzList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglTysjBdzMapper.queryTysjBdzCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());

        List<TJsglTysjBdz> list = new ArrayList<>();
        try{
            list = tJsglTysjBdzMapper.queryTysjBdzList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }

        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Object> queryTcqkBdzList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglTysjBdzMapper.queryTcqkBdzCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());

        List<TJsglTysjBdz> list = new ArrayList<>();
        try{
            list = tJsglTysjBdzMapper.queryTcqkBdzList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }

        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Object> queryMkhbdzCount(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        try{
            result = tJsglTysjBdzMapper.queryMkhbdzCount(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        return result;
    }

    @Override
    public void deleteTysjBdzRecord(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }

        try {
            tJsglTysjBdzMapper.deleteTysjBdzRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }

    @Override
    public void deleteTcqkBdzRecord(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }

        try {
            tJsglTysjBdzMapper.deleteTcqkBdzRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }
}
